package chapter_23.security;

/**
 * 多线程同步(访问"临界资源")
 * (synchronized 修饰方法)
 */
public class TicketDB {

    // 机票的数量
    private int ticketCount = 5;

    // 获取当前机票数量
    public synchronized int getTicketCount(){
        return ticketCount;
    }

    // 销售机票
    public synchronized void sellTicket(){
        try{
            // 等待用户付款
            // 线程休眠,阻塞当前线程,模拟等待用户付款
            Thread.sleep(1000);
        }catch (InterruptedException e){
            System.out.println("线程执行异常!");
        }

        System.out.printf("第%d号票,已经售出\n", ticketCount);
        ticketCount--;

    }


}
